"""
進入爱逛卖家版
"""
import time

import yaml
from uiautomator2 import xpath


from page_object.guang_page import GuangPage
from resource.path_info import *

"""
预告管理
"""
class PreviewManagerPage(GuangPage):


    def get_preview_title(self, d):
        print("get_preview_title")
        if d.xpath(manager_preview_title_xpath %1).exists:
            return d.xpath(manager_preview_title_xpath %1).get_text()


    """
    share直播预告
    """
    def share_preview_broadcast(self, d):
        print("take_head_image")
        if d.xpath(share_preview_card_xpath %1).exists:
            d.xpath(share_preview_card_xpath %1).click()
            d.xpath(preview_card_xpath).wait(timeout=10.0)
            if d.xpath(preview_card_xpath).exists:
                print("share card successfully")
                d.press("back")
            else:
                raise Exception("share card failed")
        return self

        """
        share直播预告
        """
    def get_data_preview_broadcast(self, d):
        print("get_data_preview_broadcast")
        if d.xpath(data_preview_xpath % 1).exists:
            d.xpath(data_preview_xpath % 1).click()
            d.xpath(preview_data_header_xpath).wait(timeout=3.0)
            if d.xpath(preview_data_header_xpath).exists:
                print("into preview data detail card successfully")
                assert int(d.xpath(preview_scan_number_xpath).get_text()) >= 0
            else:
                raise Exception("into preview data detail failed")
        d.press("back")
        return self

    """
    编辑预告
    """
    def edit_preview_broadcast(self, d, editTitle):
        print("edit_preview_broadcast")
        if d.xpath(edit_preview_xpath % 1).exists:
            d.xpath(edit_preview_xpath % 1).click()
            d.xpath(preview_introduce_xpath).click()
            d(focused=True).send_keys(editTitle)
            d.swipe(0.7, 0.5, 0.7, 0.03, 0.1)
            d.xpath(save_create_btn_xpath).click()
        return self

    """
    对比编辑预告结果
    """
    def check_edit_preview_broadcast(self, d, editTitle):
        print("check_preview_create")
        d.xpath(edit_preview_xpath % 1).wait(timeout=3.0)
        if d.xpath(edit_preview_xpath % 1).exists:
            d.xpath(edit_preview_xpath % 1).click()
            assert editTitle == d.xpath(preview_introduce_xpath).get_text()
            d.press("back")
        return self

    def delete_preview_broadcast(self, d):
        print("delete_preview_broadcast")
        d.xpath(more_action_preview_xpath % 1).wait(timeout=3.0)
        if d.xpath(more_action_preview_xpath % 1).exists:
            preview_id = d.xpath(preview_id_xpath %1).get_text()[5:]
            d.xpath(more_action_preview_xpath % 1).click()
            d.xpath(delete_preview_xpath).click()
            #取消删除
            d.xpath(cancel_alert_xpath).click()
            assert preview_id == d.xpath(preview_id_xpath %1).get_text()[5:]
            #确认删除
            d.xpath(more_action_preview_xpath % 1).click()
            d.xpath(delete_preview_xpath).click()
            d.xpath(ensure_alert_xpath).click()
            time.sleep(2)
            if d.xpath(preview_id_xpath % 1).exists:
                assert preview_id != d.xpath(preview_id_xpath % 1).get_text()[5:]
            d.press("back")
        return self

    """
    对比编辑预告结果
    """
    def start_preview_broadcast(self, d):
        print("start_preview_broadcast")
        if d.xpath(pre_start_preview_xpath % 1).exists:
            d.xpath(pre_start_preview_xpath % 1).click()
            d.xpath(ensure_start_video_btn_xpath).click()
            d.xpath(end_live_broadcast_btn_xpath).wait(timeout=10.0)
            assert True == d.xpath(end_live_broadcast_btn_xpath).exists
            self.end_broadcast(d)
        d.press("back")
        return self

    """
    结束直播
    """

    def end_broadcast(self, d):
        print("end_broadcast")
        d.xpath(end_live_broadcast_btn_xpath).click()
        d.xpath(ensure_alert_xpath).click()
        d.xpath(end_broadcast_title_xpath).wait(timeout=3.0)
        assert True == d.xpath(end_broadcast_title_xpath).exists
        d.xpath(end_broadcast_back_xpath).click()
        time.sleep(2)
        return self



